<?php
// +----------------------------------------------------------------------
// | Class  视图基类（CMS版）
// +----------------------------------------------------------------------
// | Copyright (c) 2023
// +----------------------------------------------------------------------

class View {

    protected $_controller;
    protected $_action;
    protected $vars;

    function __construct($controller, $action) {
        $this->_controller = $controller;
        $this->_action = $action;
        $this->vars = array();
        $controllerArr = explode('_', $this->_controller);
        $this->vars['tmplUrl'] = strtolower(end($controllerArr));
    }
	
	/**
	 * @fun   设置变量方法
	 * @desc  
	 */
    function set($name, $value) {
        //$this->$name = $value;
        //$this->variables[$name] = $value;
        $this->vars[$name] = $value;
    }

    /**
     * @fun   显示视图
     * @desc
     */
    function render($template, $flag) {
        //从数组中将变量导入到当前的符号表。
        extract($this->vars);

        // 默认模版
        $controller = strtolower($this->_controller);
        $action = strtolower($this->_action);

        // 视图指定模版时覆盖默认模版，最多两层（允许当前控制器下模版和跨控制器模版不允许映射到其他模块下的模版）
        if($template){
            $actionArr = explode('/',$template);
            $action = $actionArr[0];
            if(count($actionArr) > 1){ // 如果包含指定模型时
                $controller = $actionArr[0];
                $action = $actionArr[1];
            }
        }

        $action = $action ? $action : 'index';
        // 模版引擎启用标识
        $tempFlag = false;
        if(SYS_APP_PRO == SYS_APP_DEFAULT){ // 默认模块时（web）
            if(SYS_TMPL_CLASS){
                $tempFlag = true;
            }
            require SYS_ROOT . 'plug/headVar.php';
        }
        //$tempFlag = false;
        // 使用模版引擎
        if($tempFlag){
            $content = $this->tempFileExist($controller, $action, SYS_TMPL_CLASS_FIX);
            $tempFileHtml = SYS_VIEW_PATH . $controller . '/';
            $tempFilePHP = SYS_PATH.SYS_TMPL_CLASS_PATH.$controller . '/';
            if(!file_exists($tempFilePHP)){ SYS_ADD_PATH($tempFilePHP); }
            $db = Model::conn('mysql');
            $newFileFix = SYS_TMPL_FIX;
            //$newFileFix = '.php';
            $tagTemp = TagTemplate::getInstance($tempFileHtml, $tempFilePHP, SYS_TMPL_CLASS_FIX, $newFileFix, $db->db_fix);
            $tagTemp->turn($action.SYS_TMPL_CLASS_FIX);
            include $tempFilePHP.$action.$newFileFix;
        }else{
            // 不使用模版
            $content = $this->tempFileExist($controller, $action, SYS_TMPL_FIX);
            if($flag){
                $commonHtml = SYS_VIEW_PATH.SYS_TMPL_PARENT.SYS_TMPL_FIX;
                $commonHtmlIn = SYS_VIEW_PATH . $controller . '/'.SYS_TMPL_PARENT.SYS_TMPL_FIX;
                if(file_exists($commonHtmlIn)) {
                    include $commonHtmlIn;
                }else if(file_exists($commonHtml)){
                    include $commonHtml;
                }
            }else{
                include $content;
            }
        }
    }

    /**
     * Notes:模版文件是否存在
     * User: ZhuHaili
     * Date: 2023/3/12
     */
    private function tempFileExist($controller, $action, $fix)
    {
        $contentPagePath = SYS_VIEW_PATH . $controller . '/' . $action . $fix;
        if (file_exists($contentPagePath)) {
            $content = $contentPagePath;
        } else {
            $errStr = " Not found $contentPagePath ";
            $BException = new BaseException();
            $BException->_error_handler(1024, $errStr, __FILE__, __LINE__);
            exit;
        }
        return $content;
    }
}